<?

/**
 *   DirWalker allow others class to walk through dir hiearhy.
 *    
 * 
 */

class DirWalker 
{


private $dirOpenHandler='_dirOpenHandler';
private $dirCloseHandler='_dirCloseHandler';
private $fileHandler='_fileHandler';
private $dirpath=array();
private $rootpath;

private $isSet_DirOpenHandler=false;
private $isSet_DirCloseHandler=false;
private $isSet_FileHandler=false;

public $deepIntoSubDir=true;

public $object;

public function __construct($object,$dir ){
   
	if(!is_object($object)) RBNS::errorstream('First parameter of constructor must be object',RBNS::RBNS_CRITICAL_ERROR);
    $this->object=$object;
	$this->rootpath = $dir;
   
}   

public function setDirOpenHandler($methodName){
	if(!method_exists($this->object,$methodName)) RBNS::errorstream('Parameter must real method name',RBNS::RBNS_CRITICAL_ERROR);
	$this->dirOpenHandler=$methodName;
	$this->isSet_DirOpenHandler=true;
}

public function setDirCloseHandler($methodName){
	if(!method_exists($this->object,$methodName)) RBNS::errorstream('Parameter must real method name',RBNS::RBNS_CRITICAL_ERROR);
	$this->dirCloseHandler=$methodName;
	$this->isSet_DirCloseHandler=true;
}

public function setFileHandler($methodName){
	if(!method_exists($this->object,$methodName)) RBNS::errorstream('Parameter must real method name',RBNS::RBNS_CRITICAL_ERROR);
	$this->fileHandler=$methodName;
	$this->isSet_FileHandler=true;
}



public function getCurrentDir(){
	return $this->dirpath[count($this->dirpath)-1];
	
}  




public function directoryWalk($dirpath)
{  
	try{ 
		
		if(!@chdir($this->rootpath.$dirpath)) return false;
	}catch(ErrorException $e){
		return false;
	}
	array_push($this->dirpath,$dirpath);
	
	$dp=opendir(".");
	
    $dirOpenHandler=$this->dirOpenHandler;
	if($this->isSet_DirOpenHandler){
    	$this->object->$dirOpenHandler($dirpath);
    }else{
		$this->$dirOpenHandler($dirpath);
    }
    
  	while( ($e=readdir($dp))!==false ){
        		
        	if ($e=='.'||$e=='..') continue;
        	
        	if (is_dir($e)) {
        		
        		if(!$this->deepIntoSubDir) continue;
        		$this->directoryWalk($dirpath."/".$e);	
        		chdir("..");
        	}
        	else{    	    
        	    
        		$filehandler=$this->fileHandler;
        		if($this->isSet_FileHandler){
			    	$this->object->$filehandler($e);
			    }else{
					$this->$filehandler($e);
			    }
        	   
        	    
        	};
	 };
	 
	$dirCloseHandler=$this->dirCloseHandler;
	 
	if($this->isSet_DirCloseHandler){
    	$this->object->$dirCloseHandler($dirpath);
    }else{
		$this->$dirCloseHandler($dirpath);
    }
	 
	 array_pop($this->dirpath);
	 closedir($dp);		
	
}



private function _dirOpenHandler($e){
    echo " <div style='margin:2 0 2 7; border:1px dotted red;padding:4px'><font color=blue>$e</font><div style='margin:2 0 2 7; border:1px solid blue; padding:4px'>";
}    

private function _dirCloseHandler($e){ 
    echo " </div></div>";
}  

private function _fileHandler($e){
    echo "<br><font color=green> $e </font>";
}



};
?>